define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basic/xzsm/index' + location.search, // add_url: 'basic/unit/add',
                    edit_url: 'basic/xzsm/edit', // del_url: 'basic/unit/del',
                    // multi_url: 'basic/unit/multi',
                    // import_url: 'basic/unit/import',
                    table: 'unit',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url, pk: 'id', // sortName: 'weigh',
                fixedColumns: true, searchFormVisible: true, fixedRightNumber: 1, columns: [[// {checkbox: true},
                    {field: 'id', title: __('Id'), operate: false},
                    {
                        field: 'title', title: __('商品名称'), operate: 'LIKE', table: table, class: 'autocontent'
                    },
                    {
                        field: 'category_id', title: __('分类'), operate: false,
                    },
                    {field: 'difference', title: __('规格'), operate: 'LIKE'},
                    {
                        field: 'cost_price', title: __('成本价'), operate: false
                    },
                    {
                        field: 'price', title: __('零售价'), operate: false
                    },
                    {field: 'stock', title: __('剩余库存'), operate: false}, {
                        field: 'createtime',
                        title: __('Createtime'),
                        operate: 'RANGE',
                        addclass: 'datetimerange',
                        autocomplete: false,
                        formatter: Table.api.formatter.datetime
                    },
                    {
                        field: 'operate',
                        title: __('Operate'),
                        table: table,
                        events: Table.api.events.operate,
                        formatter: Table.api.formatter.operate
                    }]]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },

        getFirstDayOfMonth: function getFirstDayOfMonth() {
            const currentDate = new Date();
            const firstDay = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
            return this.formatDate(firstDay)
        }, getLastDayOfMonth: function getLastDayOfMonth() {
            const currentDate = new Date();
            const lastDay = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);
            return this.formatDate(lastDay);
        }, formatDate: function formatDate(date) {
            const year = date.getFullYear();
            const month = ("0" + (date.getMonth() + 1)).slice(-2);
            const day = ("0" + date.getDate()).slice(-2);
            return year + "-" + month + "-" + day;
        }
    };
    return Controller;
});
